/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/container
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AEArrayUtil.inl
// Author:		Gianluca Belardelli
// Date:		27/05/2014
//
/////////////////////////////////////////////////////////////////////////
#ifndef _AEARRAYUTIL_INL_
#define _AEARRAYUTIL_INL_

template<typename T>
AE_FORCEINLINE void AEArrayUtil::Construct( T *tElem, AEINT32 nNumElem, AETypeIsPod )
{
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::Construct( T *tElem, AEINT32 nNumElem, AETypeIsClass )
{
	for( int i = 0; i < nNumElem; ++i )
		::new ( reinterpret_cast<AEPlacementNewArg*>( tElem + i ) ) T;
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::ConstructWithCopy( T *tElem, AEINT32 nNumElem, const T &tCopy, AETypeIsPod )
{
	for( int i = 0; i < nNumElem; ++i)
		tElem[i] = tCopy;
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::ConstructWithCopy( T *tElem, AEINT32 nNumElem, const T &tCopy, AETypeIsClass )
{
	for(int i = 0; i < nNumElem; ++i)
	{
		void *lpBuffer = (void*) AEAddByteOffsetConst( tElem, sizeof(T) * i );
		::new ( reinterpret_cast<AEPlacementNewArg*>( lpBuffer ) ) T( tCopy );
	}
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::Destruct( T *tElem, AEINT32 nNumElem, AETypeIsPod )
{
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::Destruct( T *tElem, AEINT32 nNumElem, AETypeIsClass )
{
	for( int i = nNumElem-1; i >= 0; --i )
		static_cast<T*>(tElem)[i].~T();
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::ConstructWithArray( T *tElem, AEINT32 nNumElem, const T *tCopy, AETypeIsPod )
{
	for( int i = 0; i < nNumElem; ++i)
		tElem[i] = tCopy;
}

template<typename T>
AE_FORCEINLINE void AEArrayUtil::ConstructWithArray( T *tElem, AEINT32 nNumElem, const T *tCopy, AETypeIsClass )
{
	for( int i = 0; i < nNumElem; ++i )
		::new ( reinterpret_cast<AEPlacementNewArg*>( tElem + i ) ) T( tCopy[i] );
}

#endif // _AEARRAYUTIL_INL_
